<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 幕小铺科技（大连）有限公司 [ http://www.XXX.com ]
// +----------------------------------------------------------------------
// | 2020/03/14 jason wang
// +----------------------------------------------------------------------
namespace app\erp\logic;
use think\facade\Hook;
use think\DB;
use util\Tree;
use think\facade\Debug;
use think\facade\Cache;
use think\db\Where;
class GoodsWarehouseStockNumlogic{

    /**
     * 根据门店id 和 skuid 查询库存
     * @param  int    $user_id   操作用户ID
     * @author jason
     * @return mixed
     */
    public function  getKucunNum($store_id, $sku_id){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'store_id' 	                                => $store_id,
                'sku_id' 	                                => $sku_id,
            ];
            /********行为参数赋值（*）********/
     
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            //根据门店id 查询出仓库的id
            $warehouse_list = model('ErpStoreWarehouse') -> getStoreWarehouse($store_id);
            foreach ($warehouse_list['data'] as $key => $value) {
                if($value['type'] == 1){//分店仓库
                    $store_warehouse_list[] = $value['wid'];
                }
            }
            //根据仓库id 查询库存数
            $map = [];
            $map = ['warehouse_id', 'in', $store_warehouse_list];
            $map = ['store_id', '=', $store_id];
            $list = model('ErpGoodsWarehouseStockNum')-> field('SUM(stock_num)') -> where($map) -> find();
            //dump($list);
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success', $result);
            /********逻辑返回********/
			
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }

	
}